package com.kimehwa.studyboot.controller;

import com.kimehwa.studyboot.bean.User;
import com.kimehwa.studyboot.exception.ExceptionCodeEnum;
import com.kimehwa.studyboot.exception.Result;
import com.kimehwa.studyboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.FutureTask;


@RestController
public class HelloController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    @PostMapping("insertUser1")
    public Result<Boolean> insertUser(@RequestBody User user){
        if (user == null) {
            return Result.error(ExceptionCodeEnum.EMPTY_PARAM);
        }
        if (user.getId() == null || user.getId() <= 0) {
            return Result.error("id为空或小于0");
        }
        if (StringUtils.isEmpty(user.getName()) || user.getName().length() > 4) {
            return Result.error("姓名不符合规范");
        }
        if (user.getAge() < 18) {
            return Result.error("年龄不小于18");
        }
        if (StringUtils.isEmpty(user.getPhone()) || user.getPhone().length() != 11) {
            return Result.error("手机号码不正确");
        }

        return Result.success(userService.save(user));
    }



}
